package main

import (
	"fmt"
	"sync"
)

var lock sync.Mutex

func finished() {
	fmt.Println("Finished finding largest")
}

func largest(nums []int) {
	fmt.Println("desfer，用途：函数返回之前，调用另一个函数")
	defer finished()
	fmt.Println("Started finding largest")
	// 默认最大值为第一个元素
	max := nums[0]
	for _, v := range nums {
		if v > max {
			max = v
		}
	}
	fmt.Println("Largest number in", nums, "is", max)
}

func closeLock() {
	m := make(map[string]interface{})
	l := sync.Mutex{}
	wg := sync.WaitGroup{}

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(i int) {
			l.Lock()
			defer l.Unlock()
			m["num"] = i
			defer wg.Done()
		}(i)
	}
	wg.Wait()
	fmt.Println(m)
}

type project struct {
	projectName string
	noOfHours   int
	hourlyRate  int
}

func test() {
	lock.Lock()
	lock.Unlock()
}

func testDefer() {
	lock.Lock()
	defer lock.Unlock()
}

func (p project) income(wg *sync.WaitGroup) {
	defer wg.Done()
	sum := p.noOfHours * p.hourlyRate
	if sum >= 100 {
		fmt.Println(p.projectName, " more than 100")
	} else {
		fmt.Println(p.projectName, " less than 100")
	}

}

func main() {

}
